package com.guoqi.oabridge.bridgeImpl

import android.app.ProgressDialog
import android.support.v7.app.AlertDialog
import android.widget.Toast
import com.alibaba.fastjson.JSONException
import com.alibaba.fastjson.JSONObject
import com.guoqi.oabridge.Callback
import com.guoqi.oabridge.Callback.Companion.setSuccess
import com.tencent.smtt.sdk.WebView

/**
 * 交互
 * @author GUOQI
 */
open class UiImpl : IBridge {

    companion object {

        private var progressDialog: ProgressDialog? = null

        /**
         * Toast
         */
        @JvmStatic
        open fun showToast(webView: WebView, param: JSONObject, callback: Callback) {
            val message = param.getString("title")
            if (!message.isNullOrEmpty()) {
                Toast.makeText(webView.context, message, Toast.LENGTH_SHORT).show()
            }
        }

        /**
         * Dialog
         */
        @JvmStatic
        open fun showDialog(webView: WebView, param: JSONObject, callback: Callback) {
            val title = param.getString("title") ?: "提示"
            val content = param.getString("content") ?: ""
            val confirmText = param.getString("confirmText") ?: "确认"
            val cancelText = param.getString("cancelText") ?: "取消"
            val showCancel = param.getBoolean("showCancel") ?: true
            val cancelable = param.getBoolean("cancelable") ?: true

            var dialog = AlertDialog.Builder(webView.context).setTitle(title)
                .setMessage(content)
                .setPositiveButton(confirmText) { _, _ ->
                    val obj = JSONObject()
                    obj["msg"] = "点击了确定"
                    callback.apply(setSuccess(obj))
                }
                .setCancelable(cancelable)

            if (showCancel) {
                dialog.setNegativeButton(cancelText) { _, _ ->
                    val obj = JSONObject()
                    obj["msg"] = "点击了取消"
                    callback.apply(setSuccess(obj))
                }
            }
            dialog.show()
        }

        /**
         * loading
         */
        @JvmStatic
        open fun showLoading(webView: WebView, param: JSONObject, callback: Callback) {
            val title = param.getString("title") ?: "加载中"
            val cancelable = param.getBoolean("cancelable") ?: true
            if (progressDialog == null) {
                progressDialog = ProgressDialog(webView.context)
                progressDialog?.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            }
            progressDialog?.setMessage(title)
            progressDialog?.setCancelable(cancelable)
            progressDialog?.show()
        }

        @JvmStatic
        open fun hideLoading(webView: WebView, param: JSONObject, callback: Callback) {
            webView.context?.let {
                if (progressDialog != null && progressDialog!!.isShowing) {
                    progressDialog?.dismiss()
                }
            }
        }

        /**
         * thread
         */
        @JvmStatic
        open fun testThread(webView: WebView, param: JSONObject, callback: Callback) {
            Thread(Runnable {
                try {
                    Thread.sleep(3000)
                    val obj = JSONObject()
                    obj["msg"] = "收到回调"
                    callback.apply(setSuccess(obj))
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            }).start()
        }
    }
}
